<template>
	<div>
		<Form ref='newsType' inline lable-width="100%">
			<Button class="addbutton" icon="plus-round" type="dashed" @click="toAddPage">新增新闻</Button>
			<br />
			<v-table ref='vTable' :tableColumns='tableColumns' :tableData='tableData' :totalPage='totalPage' :isPage='isPage' @getPage='selectPage'></v-table>
		</Form>
	</div>
</template>

<script>
	import vTable from '../../../common/Table.vue'
	export default {
		components:{
			vTable
		},
		data(){
			return {
				isAddShow: false,
				isKeep: false,
				formData:{
					id: '',   // 新闻ID
			        content: '',
			        description: '',
			        name: '',
			        pic: '',
			        seqNo: 1,  //新闻排序
			        categoryId: "1", //新闻类别ID
			        page:0,
			        limit: 10
				},
				//  验证规则
				HealthCarRules:{
					name: [{ required: true, message: "请输入标题" }]
				},
				tableColumns:[
					{ type: 'index', width: 60,  align: 'center' },
					{ title: '日期', key: 'created_time', align: 'center' },
					{ title: '标题', key: 'name', align: 'center' },
					{ title: '发布', key: 'status', align: 'center' },
					{
                        title: '操作',
                        key: 'action',
                        width: 180,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                            h('Button', {
                                    props: { type: 'primary', size: 'small' },
                                    style: { marginRight: '10px' },
                                    on: {
                                        click: () => {
                                        	// 发布新闻
                                        	this.$http.post('/news/release',{newsId: params.row.id})
                                        	.then((res)=>{
                                        		console.log(res)
                                        		this.getData('/news/detail_list',{categoryId: 1})
                                        		this.$Message.success('发布新闻成功!');
                                        	})
                                        	.catch((err)=>{
                                        		console.log(err);
                                        		this.$Message.error('发布新闻失败!');
                                        	})
                                        }
                                    }
                                }, '发布'),
                                h('Button', {
                                    props: { type: 'primary', size: 'small' },
                                    style: { marginRight: '10px' },
                                    on: {
                                        click: () => {
                                        	this.formData = JSON.parse(
                                        		JSON.stringify(params.row)
                                        	)
											this.formData.seqNo =  this.formData.seq_no;
                                        	this.$router.push({
                                        		path:'/news/list/edit/' + params.row.id,
                                        		query: this.formData
                                        	})
                                     //   	console.log(this.formData)
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {  type: 'error', size: 'small' },
                                    on: {
                                        click: () => {
                                        	// 删除新闻
//                                      	console.log(params.row.id)
                                        	this.$http.post('/news/remove',{newsId: params.row.id})
                                        	.then(({data})=>{
                                        		console.log(data)
                                        		this.getData('/news/detail_list',{categoryId: 1})
                                        		this.$Message.success('删除新闻成功!');
                                        	})
                                        	.catch((err)=>{
                                        		console.log(err)
                                        	})
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
				],
				tableData:[],
				totalPage: 0,
				isPage: false
			}
		},
		created(){
			//  初始化数据
			this.getData('/news/detail_list',{categoryId: 1})
		},
		methods:{
			// 获取数据
			getData(url,jsonData){
				this.$http.post(url,jsonData)
				.then((res)=>{
					this.tableData = res.data.news.map((item)=>{
						item.categoryId = 1;
						item.created_time= new Date(parseInt(item.created_time)).toLocaleDateString();	//  时间戳转日期
						return item;
					})
					// 分页
					this.totalPage = res.data.paginator.totalCount;
					if(res.data.paginator.totalPages > 1){
						this.isPage = true;
					} else {
						this.isPage = false;
					}
				//	console.log(this.tableData)
					
				})
				.catch((err)=>{
					console.log(err)
					this.$Notice.error({
						title: '获取新闻数据失败!'
					})
				})
			},
			// 去到新增新闻页面
			toAddPage(){
				this.$router.push({path: '/news/list'});
			},
			// 选页
			selectPage(pageNum){
			//	console.log(pageNum)
				this.getData('/news/detail_list',{ limit: 10, page: pageNum, categoryId:1 })
			}
		}
	}
</script>
<style>
	.header{
		font-size: 16px;
		text-align: center;
		color: #0b9595;
		margin-bottom: 20px;
	}
</style>